package com.shrct.businessprocess.purchase.impl;

import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.project.RCCustomerContract;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCRealMoney;
import com.shrct.businessprocess.purchase.RCProjectContractSummary;
import com.shrct.businessprocess.purchase.RCProjectIncomeService;
import com.shrct.businessprocess.purchase.RCPurchaseIncomeProcess;
import com.shrct.businessprocess.purchase.RCPurchaseIncomeRequest;
import com.shrct.businessprocess.purchase.impl.RCPurchaseIncomeProcessImpl;
import edu.fudan.mylang.pf.BaseService;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.List;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.purchase.RCProjectIncomeService")
public class RCProjectIncomeServiceImpl extends BaseService implements RCProjectIncomeService {
  @Autowired
  private IObjectFactory objectFactory;
  
  public RCPurchaseIncomeProcess startIncomeReqest(final RCProject project, final String comment, final RCCustomerContract customerContract, final Procedure1<? super RCPurchaseIncomeRequest> init) {
    RCPurchaseIncomeProcess _create = RCPurchaseIncomeProcessImpl.create(this.objectFactory);
    final Procedure1<RCPurchaseIncomeProcess> _function = new Procedure1<RCPurchaseIncomeProcess>() {
        public void apply(final RCPurchaseIncomeProcess it) {
          it.start(project, customerContract, comment, init);
        }
      };
    RCPurchaseIncomeProcess _doubleArrow = ObjectExtensions.<RCPurchaseIncomeProcess>operator_doubleArrow(_create, _function);
    return _doubleArrow;
  }
  
  public Collection<RCProjectContractSummary> getProjectContractSummary(final RCProject project) {
    Collection<RCCustomerContract> _contracts = project.getContracts();
    final Function1<RCCustomerContract,RCProjectContractSummary> _function = new Function1<RCCustomerContract,RCProjectContractSummary>() {
        public RCProjectContractSummary apply(final RCCustomerContract contract) {
          RCProjectContractSummary _rCProjectContractSummary = new RCProjectContractSummary();
          final Procedure1<RCProjectContractSummary> _function = new Procedure1<RCProjectContractSummary>() {
              public void apply(final RCProjectContractSummary it) {
                RCEnterprise _customer = contract.getCustomer();
                String _entityName = _customer.getEntityName();
                it.setCustomerName(_entityName);
                it.setCustomerContract(contract);
                RCProjectMoney _totalContractAmount = project.getTotalContractAmount(contract);
                it.setTotalContractAmount(_totalContractAmount);
                RCProjectMoney _totalInvoiceAmount = project.getTotalInvoiceAmount(contract);
                it.setTotalInvoiceAmount(_totalInvoiceAmount);
                RCProjectMoney _totalInvoiceAndUnderInvoiceAmount = contract.getTotalInvoiceAndUnderInvoiceAmount();
                it.setTotalInvoiceAndUnderInvoiceAmount(_totalInvoiceAndUnderInvoiceAmount);
                RCRealMoney _totalIncomeAmount = project.getTotalIncomeAmount(contract);
                it.setTotalIncomeAmount(_totalIncomeAmount);
              }
            };
          RCProjectContractSummary _doubleArrow = ObjectExtensions.<RCProjectContractSummary>operator_doubleArrow(_rCProjectContractSummary, _function);
          return _doubleArrow;
        }
      };
    Iterable<RCProjectContractSummary> _map = IterableExtensions.<RCCustomerContract, RCProjectContractSummary>map(_contracts, _function);
    List<RCProjectContractSummary> _list = IterableExtensions.<RCProjectContractSummary>toList(_map);
    return _list;
  }
}
